﻿using Biotech.Utils;
using project.B12.IRepository;
using project.B12.IService;
using project.B12.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace project.B12.Service
{
    /// <summary>
    /// conf 文件夹下的配置文件管理
    /// </summary>
    public class ConfigurationFileManagement : IConfigurationFileManagement
    {
        public  InstrumentConf InstrumentConf { get ; set ; }

        //public  List<MicroplateConf> microplateConfs { get ; set ; }

        public ConfigurationFileManagement()
        {
            try
            {
                if (!Directory.Exists("Image"))
                {
                    DirectoryInfo dirInfo = new DirectoryInfo("Image");
                    dirInfo.Create();
                }
            }
            catch  { }

            if(!Directory.Exists("conf"))
                throw new Exception($"Directory 'conf' not found.文件缺失或异常");

            InstrumentConf = new InstrumentConf();
            InstrumentConf.AreaX     = int.Parse(iniUtil.INIRead("motor", "areaX", InstrumentConf.MotorConfigPath));
            InstrumentConf.AreaY     = int.Parse(iniUtil.INIRead("motor", "areaY", InstrumentConf.MotorConfigPath));
            InstrumentConf.offsetX   = int.Parse(iniUtil.INIRead("motor", "offsetX", InstrumentConf.MotorConfigPath));
            InstrumentConf.offsetY   = int.Parse(iniUtil.INIRead("motor", "offsetY", InstrumentConf.MotorConfigPath));
            InstrumentConf.stopX     = int.Parse(iniUtil.INIRead("motor", "stopX", InstrumentConf.MotorConfigPath));
            InstrumentConf.stopY     = int.Parse(iniUtil.INIRead("motor", "stopY", InstrumentConf.MotorConfigPath));
            InstrumentConf.Power     = ushort.Parse(iniUtil.INIRead("led", "power", InstrumentConf.MotorConfigPath));
            InstrumentConf.moveDelay = ushort.Parse(iniUtil.INIRead("led", "move delay", InstrumentConf.MotorConfigPath));
            InstrumentConf.lighDelay = ushort.Parse(iniUtil.INIRead("led", "light delay", InstrumentConf.MotorConfigPath));
            InstrumentConf.Exposure  = ushort.Parse(iniUtil.INIRead("camera", "exposure", InstrumentConf.MotorConfigPath));
            InstrumentConf.key       = ushort.Parse(iniUtil.INIRead("AI", "key", InstrumentConf.MotorConfigPath));
            #region
            //DirectoryInfo folder     = new DirectoryInfo("conf");
            //FileInfo[] fils          = folder.GetFiles("*.ini");
            //microplateConfs = new List<MicroplateConf>();
            //foreach (FileInfo file in fils)
            //{
            //    if (file.Name.Contains("Microplate"))
            //    {
            //        microplateConfs.Add(new MicroplateConf(file.Name));
            //    }
            //}

            //if (microplateConfs.Count > 0)
            //{
            //    foreach (var micF in microplateConfs)
            //    {
            //        micF.name = iniUtil.INIRead("type", "name", micF.Microplate_ConfigPath);
            //        micF.X = float.Parse(iniUtil.INIRead("motor", "X", micF.Microplate_ConfigPath));
            //        micF.Y = float.Parse(iniUtil.INIRead("motor", "Y", micF.Microplate_ConfigPath));
            //        micF.R = float.Parse(iniUtil.INIRead("motor", "R", micF.Microplate_ConfigPath));
            //        micF.U = float.Parse(iniUtil.INIRead("motor", "U", micF.Microplate_ConfigPath));
            //        micF.holeX1 = int.Parse(iniUtil.INIRead("cut", "holeX1", micF.Microplate_ConfigPath));
            //        micF.holeX2 = int.Parse(iniUtil.INIRead("cut", "holeX2", micF.Microplate_ConfigPath));
            //        micF.holeX3 = int.Parse(iniUtil.INIRead("cut", "holeX3", micF.Microplate_ConfigPath));
            //        micF.holeX4 = int.Parse(iniUtil.INIRead("cut", "holeX4", micF.Microplate_ConfigPath));
            //        micF.holeX5 = int.Parse(iniUtil.INIRead("cut", "holeX5", micF.Microplate_ConfigPath));
            //        micF.holeX6 = int.Parse(iniUtil.INIRead("cut", "holeX6", micF.Microplate_ConfigPath));
            //        micF.holeX7 = int.Parse(iniUtil.INIRead("cut", "holeX7", micF.Microplate_ConfigPath));
            //        micF.holeX8 = int.Parse(iniUtil.INIRead("cut", "holeX8", micF.Microplate_ConfigPath));
            //        micF.holeX9 = int.Parse(iniUtil.INIRead("cut", "holeX9", micF.Microplate_ConfigPath));
            //        micF.holeX10 = int.Parse(iniUtil.INIRead("cut", "holeX10", micF.Microplate_ConfigPath));
            //        micF.holeX11 = int.Parse(iniUtil.INIRead("cut", "holeX11", micF.Microplate_ConfigPath));
            //        micF.holeX12 = int.Parse(iniUtil.INIRead("cut", "holeX12", micF.Microplate_ConfigPath));
            //        micF.holeX13 = int.Parse(iniUtil.INIRead("cut", "holeX13", micF.Microplate_ConfigPath));
            //        micF.holeX14 = int.Parse(iniUtil.INIRead("cut", "holeX14", micF.Microplate_ConfigPath));
            //        micF.holeX15 = int.Parse(iniUtil.INIRead("cut", "holeX15", micF.Microplate_ConfigPath));
            //        micF.holeX16 = int.Parse(iniUtil.INIRead("cut", "holeX16", micF.Microplate_ConfigPath));
            //        micF.holeY1 = int.Parse(iniUtil.INIRead("cut", "holeY1", micF.Microplate_ConfigPath));
            //        micF.holeY2 = int.Parse(iniUtil.INIRead("cut", "holeY2", micF.Microplate_ConfigPath));
            //        micF.holeY3 = int.Parse(iniUtil.INIRead("cut", "holeY3", micF.Microplate_ConfigPath));
            //        micF.holeY4 = int.Parse(iniUtil.INIRead("cut", "holeY4", micF.Microplate_ConfigPath));
            //        micF.holeY5 = int.Parse(iniUtil.INIRead("cut", "holeY5", micF.Microplate_ConfigPath));
            //        micF.holeY6 = int.Parse(iniUtil.INIRead("cut", "holeY6", micF.Microplate_ConfigPath));
            //        micF.holeY7 = int.Parse(iniUtil.INIRead("cut", "holeY7", micF.Microplate_ConfigPath));
            //        micF.holeY8 = int.Parse(iniUtil.INIRead("cut", "holeY8", micF.Microplate_ConfigPath));
            //        micF.holeY9 = int.Parse(iniUtil.INIRead("cut", "holeY9", micF.Microplate_ConfigPath));
            //        micF.holeY10 = int.Parse(iniUtil.INIRead("cut", "holeY10", micF.Microplate_ConfigPath));
            //        micF.holeY11 = int.Parse(iniUtil.INIRead("cut", "holeY11", micF.Microplate_ConfigPath));
            //        micF.holeY12 = int.Parse(iniUtil.INIRead("cut", "holeY12", micF.Microplate_ConfigPath));
            //        micF.holeY13 = int.Parse(iniUtil.INIRead("cut", "holeY13", micF.Microplate_ConfigPath));
            //        micF.holeY14 = int.Parse(iniUtil.INIRead("cut", "holeY14", micF.Microplate_ConfigPath));
            //        micF.holeY15 = int.Parse(iniUtil.INIRead("cut", "holeY15", micF.Microplate_ConfigPath));
            //        micF.holeY16 = int.Parse(iniUtil.INIRead("cut", "holeY16", micF.Microplate_ConfigPath));
            //        micF.dx = int.Parse(iniUtil.INIRead("cut", "dx", micF.Microplate_ConfigPath));
            //        micF.dy = int.Parse(iniUtil.INIRead("cut", "dy", micF.Microplate_ConfigPath));
            //        micF.width = int.Parse(iniUtil.INIRead("cut", "width", micF.Microplate_ConfigPath));
            //        micF.height = int.Parse(iniUtil.INIRead("cut", "height", micF.Microplate_ConfigPath));
            //        micF.Rr = int.Parse(iniUtil.INIRead("Algorithm", "R", micF.Microplate_ConfigPath));
            //        micF.Square = int.Parse(iniUtil.INIRead("Algorithm", "Square", micF.Microplate_ConfigPath));
            //        micF.Fill = int.Parse(iniUtil.INIRead("Algorithm", "Fill", micF.Microplate_ConfigPath));
            //        micF.grayValueCo = int.Parse(iniUtil.INIRead("Algorithm", "grayValueCo", micF.Microplate_ConfigPath));
            //        micF.PixelCo = int.Parse(iniUtil.INIRead("Algorithm", "PixelCo", micF.Microplate_ConfigPath));
            //        micF.CVCo = float.Parse(iniUtil.INIRead("Algorithm", "CVCo", micF.Microplate_ConfigPath));
            //    }
            //    microplateConfs.OrderBy(m => m.fileSName);
            //}
            #endregion
        }

        private class MapConfig
        {
            public Map[] Maps { get; set; }
        }

        private readonly MapConfig MC = yamlUtil.Deserializer<MapConfig>(@"conf/conf_map.yml");
 
 
        public Map[] GetMapCfg()
        {
            return MC.Maps;
        }

        public Map GetMapCfgByName(int pName)
        {
            foreach (var item in MC.Maps)
            {
                if (item.Name == pName)
                    return item;
            }
            throw new NotImplementedException();
        }


        private class ReadConfig
        {
            public Read[] Reads { get; set; }
        }

        private readonly ReadConfig RC = yamlUtil.Deserializer<ReadConfig>(@"conf/conf_read.yml");


        public Read[] GetReadCfg()
        {
            return RC.Reads;
        }

        public Read GetReadCfgByName(int pName)
        {
            foreach (var item in RC.Reads)
            {
                if (item.Name == pName)
                    return item;
            }
            throw new NotImplementedException();
        }


        private class PlateTypeConfig
        {
            public PlateType[] Types { get; set; }
        }

        private readonly PlateTypeConfig PC = yamlUtil.Deserializer<PlateTypeConfig>(@"conf/conf_type.yml");


        public  PlateType[] GetPlateTypeCfg()
        {
            return PC.Types;
        }

        public  PlateType GetPlateTypeCfgByName(int pName)
        {
            foreach (var item in PC.Types)
            {
                if (item.Name == pName)
                    return item;
            }
            throw new NotImplementedException();
        }


    }
}
